using ASP;
using IPMS.BLL;
using Resources;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Common;
using System.Data.SqlClient;
using System.Globalization;
using System.Threading;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public partial class RespPerson : Page, IRequiresSessionState
{
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        this.clearText();
        this.setTextReadOnly(true);
        this.setButtons(string.Empty);
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        Project runMethod = new Project();
        bool superUser = Convert.ToBoolean(this.Session["SuperUser"].ToString());
        runMethod.GetProjectStatus(this.Session["ProjectCode"].ToString(), superUser, this.Session["OwnerGroupId"].ToString());
        if (runMethod.Status == "Expired")
        {
            this.lblError.Visible = true;
            this.lblError.Text = Messages.DELETE_ProjectExpired.ToString();
        }
        else
        {
            string projectCode = this.Session["ProjectCode"].ToString();
            this.ODSRespPerson2.DeleteMethod = "Delete";
            this.ODSRespPerson2.DeleteParameters["ProjectCode"].DefaultValue = projectCode;
            this.ODSRespPerson2.DeleteParameters["ResponsiblePerson"].DefaultValue = this.ddlRespPerson.SelectedValue.ToString();
            this.ODSRespPerson2.Delete();
            this.gvRespPersons.DataBind();
            this.btnCancel_Click(sender, e);
        }
    }

    protected void btnModify_Click(object sender, EventArgs e)
    {
        this.lblError.Visible = true;
        this.lblError.Text = "Update is not allowed on primary keys. ";
    }

    protected void btnNew_Click(object sender, EventArgs e)
    {
        Project runMethod = new Project();
        bool superUser = Convert.ToBoolean(this.Session["SuperUser"].ToString());
        runMethod.GetProjectStatus(this.Session["ProjectCode"].ToString(), superUser, this.Session["OwnerGroupId"].ToString());
        if (runMethod.Status == "Expired")
        {
            this.lblError.Visible = true;
            this.lblError.Text = Messages.ADD_ProjectExpired.ToString();
        }
        else
        {
            this.clearText();
            this.setTextReadOnly(false);
            this.setButtons("new");
            this.ddlRespPerson.DataBind();
            this.ddlRespPerson.Focus();
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string projectCode = this.Session["ProjectCode"].ToString();
        string ownerGroupID = this.Session["OwnerGroupID"].ToString();
        try
        {
            this.ODSRespPerson2.InsertMethod = "Insert";
            this.ODSRespPerson2.InsertParameters["ProjectCode"].DefaultValue = projectCode;
            this.ODSRespPerson2.InsertParameters["ResponsiblePerson"].DefaultValue = this.ddlRespPerson.SelectedValue.ToString();
            this.ODSRespPerson2.InsertParameters["CreateBy"].DefaultValue = this.Page.User.Identity.Name.ToString();
            this.ODSRespPerson2.InsertParameters["UpdateBy"].DefaultValue = this.Page.User.Identity.Name.ToString();
            this.ODSRespPerson2.InsertParameters["OwnerGroupID"].DefaultValue = ownerGroupID;
            this.ODSRespPerson2.Insert();
            this.ODSRespPersons.DataBind();
            this.gvRespPersons.DataBind();
        }
        catch (Exception ex)
        {
            this.lblError.Visible = true;
            List<string> lstValues = new List<string>();
            //this.lblError.Text = IPDataFormat.GenerateMessage(ex, lstValues);
            this.lblError.Text = ex.Message.ToString();
            return;
        }
        this.btnCancel_Click(sender, e);
    }

    protected void clearText()
    {
        this.lblError.Text = "";
        this.lblError.Visible = false;
        this.ddlRespPerson.SelectedIndex = -1;
    }

    protected void gvRespPersons_RowCreated(object sender, GridViewRowEventArgs e)
    {
        this.Sort(e, this.gvRespPersons);
    }

    protected void gvRespPersons_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            this.ddlRespPerson.SelectedIndex = -1;
            this.ddlRespPerson.Items.FindByValue(this.gvRespPersons.DataKeys[this.gvRespPersons.SelectedIndex].Values["ResponsiblePerson"].ToString()).Selected = true;
        }
        catch (NullReferenceException)
        {
        }
        catch (ArgumentOutOfRangeException)
        {
        }
    }

    protected override void InitializeCulture()
    {
        string sCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();
        try
        {
            sCulture = this.Session["Culture"].ToString();
        }
        catch (NullReferenceException)
        {
        }
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(sCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(sCulture);
        base.InitializeCulture();
    }

    protected void ODSRespPersons_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        SqlParameter[] sqlparam = new SqlParameter[] { new SqlParameter() };
        sqlparam[0].ParameterName = "@ProjectCode";
        sqlparam[0].Value = this.Session["ProjectCode"].ToString();
        DbParameter[] param = sqlparam;
        e.InputParameters[0] = "_IPMS_RespPersonSp";
        e.InputParameters[1] = param;
    }

    protected void ODSUsers_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.Page.IsPostBack)
        {
            this.btnDelete.Attributes.Add("onClick", "return confirm('" + Messages.DeleteConfirmation.ToString() + "')");
        }
        this.setTextReadOnly(true);
        this.setButtons(string.Empty);
    }

    protected void Page_UnLoad(object sender, EventArgs e)
    {
    }

    public void setButtons(string action)
    {
        switch (action)
        {
            case "new":
                this.btnNew.Enabled = false;
                this.btnModify.Enabled = false;
                this.btnSave.Enabled = true;
                this.btnDelete.Enabled = false;
                return;

            case "modify":
                this.btnNew.Enabled = false;
                this.btnModify.Enabled = false;
                this.btnSave.Enabled = true;
                this.btnDelete.Enabled = false;
                return;
        }
        this.btnNew.Enabled = true;
        this.btnModify.Enabled = !this.ddlRespPerson.Enabled;
        this.btnSave.Enabled = false;
        this.btnDelete.Enabled = !this.ddlRespPerson.Enabled;
    }

    protected void setTextReadOnly(bool bBool)
    {
        this.ddlRespPerson.Enabled = !bBool;
    }

    protected void Sort(GridViewRowEventArgs e, GridView gv)
    {
        if ((e.Row != null) && (e.Row.RowType == DataControlRowType.Header))
        {
            foreach (TableCell cell in e.Row.Cells)
            {
                if (cell.HasControls())
                {
                    LinkButton button = (LinkButton)cell.Controls[0];
                    if (button != null)
                    {
                        Image image = new Image();
                        image.ImageUrl = "";
                        if (gv.SortExpression == button.CommandArgument)
                        {
                            if (gv.SortDirection == SortDirection.Ascending)
                            {
                                image.ImageUrl = "images/sortup.gif";
                            }
                            else
                            {
                                image.ImageUrl = "images/sortdown.gif";
                            }
                        }
                        if (image.ImageUrl != "")
                        {
                            cell.Controls.Add(image);
                        }
                    }
                }
            }
        }
    }

}
